<?php
namespace Admin\Controller;

class PermController extends BaseController {

	public function index() {   
        $this->display();
    }


    public function get_perm(){
	  	$this->verify_request();

	  	$page = $_POST['page'];
	    $rows = $_POST['rows'];
	    $limit = ($page-1)*$rows.','.$rows;

	  	$Perm = D("Perm");
	  	$permList = $Perm->limit($limit)->select();
	    if(empty($permList))
	        $permList = false;
	  	$count = $Perm->count();
	  	$this->ajaxReturn(array("total"=>$count,"rows"=>$permList));
	  }

  	public function save() {

  		$Perm = D("Perm");

        $data['name'] = !empty($_POST['name'])?$_POST['name']:NULL;
        if (empty($data['name']))
            $this->ajaxReturn(array('errorMsg' => "权限名称不能为空"));

        $data['desc'] = $_POST['desc'];
        $data['controller'] = $_POST['controller'];
        // 添加
        if ($flag = $Perm->add($data) !== false)
          $this->ajaxReturn(array('success' => true));
        else $this->ajaxReturn(array('errorMsg'=>'添加失败'));

    }

    public function update() {

        $Perm = D("Perm");

        $id = !empty($_GET['id'])?$_GET['id']:NULL;
        if (empty($id)) $this->ajaxReturn(array('errorMsg'=>'未知权限信息'));
        if (!$rs = $Perm->find($id)) $this->ajaxReturn(array('errorMsg'=>'权限不存在'));

        $data['name'] = $_POST['name'];
        $data['desc'] = $_POST['desc'];
        $data['controller'] = $_POST['controller'];

        if ($flag = $Perm->where('id = '.$id)->save($data) !== false)
            $this->ajaxReturn(array('success'=>true));
        $this->ajaxReturn(array('errorMsg'=>'修改权限失败'));
    }

    public function del() {

        $Perm = D("Perm");

        $id = $_POST['id'];
        if (empty($id)) $this->ajaxReturn(array('errorMsg'=>'未知权限'));
        if (!$rs = $Perm->find($id)) $this->ajaxReturn(array('errorMsg'=>'权限不存在'));

        if (!$Perm->delete($id)) $this->ajaxReturn(array('errorMsg'=>'删除失败'));
        $this->ajaxReturn(array('success'=>true));
    }

    public function get_perm_select(){
    	$Perm = D("Perm");
    	$this->ajaxReturn($Perm->select());
    }


}